//
//  UITextField.swift
//  Documentation
//
//  Created by Toj on 12/2/22.
//

@available(iOS 2.0, *)
open class UITextField : UIControl, UITextInput, NSCoding, UIContentSizeCategoryAdjusting {
    
    // 文本. 默认nil
    open var text: String? = nil
    
    // 富文本. 默认nil
    @NSCopying open var attributedText: NSAttributedString? = nil
    
    // 文本颜色. 默认黑色
    open var textColor: UIColor? = .black
    
    // 文本字体. 默认: 系统12号字体
    open var font: UIFont? = UIFont.systemFont(ofSize: 12)
    
    // 文本对齐方式. 默认: 左侧
    open var textAlignment: NSTextAlignment = .left
    
    // 边框. 默认: .none.
    // 如果设置为 .roundedRect, 自定义背景图像将被忽略.
    open var borderStyle: UITextField.BorderStyle = .none
    
    @available(iOS 7.0, *)
    // 默认富文本属性一些值
    open var defaultTextAttributes: [NSAttributedString.Key : Any]
    
    
    // 占位文本, 当text为nil的时候, 展示的文本. 默认: nil.
    // 默认颜色: gray 70%
    // OC可以通过KVC 设置颜色值. Swift通过KVC会闪退, 但是可以通过 attributedPlaceholder 设置
    open var placeholder: String? = nil
    
    @available(iOS 6.0, *)
    // 占位文本的富文本属性
    @NSCopying open var attributedPlaceholder: NSAttributedString?
    
    
    // 再次编辑时是否清空之前内容. 默认: false
    open var clearsOnBeginEditing: Bool = false
    
    // 自适应宽度. 默认: false
    // true: 当充满边框时, 文字会缩小, 当小到一定程度时仍然会滚动
    open var adjustsFontSizeToFitWidth: Bool = false
    
    // adjustsFontSizeToFitWidth = true
    // 当可以滚动的时候, 设置滚动时最小字号, 否则没意义
    open var minimumFontSize: CGFloat = 0
    
    // 代理
    weak open var delegate: UITextFieldDelegate? = nil
    
    // 背景图片
    open var background: UIImage? = nil
    // 不可用时的背景图
    open var disabledBackground: UIImage? = nil
    
    // 是否编辑中?
    open var isEditing: Bool { get }
    
    @available(iOS 6.0, *)
    open var allowsEditingTextAttributes: Bool // default is NO. allows editing text attributes with style operations and pasting rich text
    
    @available(iOS 6.0, *)
    open var typingAttributes: [NSAttributedString.Key : Any]? // automatically resets when the selection changes
    
    
    // 设置清除按钮显示模式
    open var clearButtonMode: UITextField.ViewMode // sets when the clear button shows up. default is UITextFieldViewModeNever
    
    // 文字左边视图
    open var leftView: UIView?
    open var leftViewMode: UITextField.ViewMode = UITextFieldViewModeNever
    
    
    // 文字右边视图
    open var rightView: UIView?
    open var rightViewMode: UITextField.ViewMode = UITextFieldViewModeNever
    
    
    // 下面几个方法 重写 后 改变绘制位置大小用到, 几乎用不到
    
    open func borderRect(forBounds bounds: CGRect) -> CGRect
    open func textRect(forBounds bounds: CGRect) -> CGRect
    open func placeholderRect(forBounds bounds: CGRect) -> CGRect
    open func editingRect(forBounds bounds: CGRect) -> CGRect
    open func clearButtonRect(forBounds bounds: CGRect) -> CGRect
    open func leftViewRect(forBounds bounds: CGRect) -> CGRect
    open func rightViewRect(forBounds bounds: CGRect) -> CGRect
    
    
    // 占位文字绘制位置大小
    open func drawPlaceholder(in rect: CGRect)
    
    
    // 弹出的键盘View(自定义键盘)
    open var inputView: UIView?
    // 系统键盘或自定义键盘上的视图
    open var inputAccessoryView: UIView?
    
    
    // 是否允许再次编辑时在内容中间插入内容. 默认: false
    @available(iOS 6.0, *)
    open var clearsOnInsertion: Bool = false
}

extension UITextField {
    
    @available(iOS 10.0, *)
    public class let didEndEditingReasonUserInfoKey: String
}

// MARK: Protocols -> UITextInput
extension UITextField: UITextInput { }
